// api/user.js
const BASE_URL = 'http://121.37.1.66:8081'; // 你的基础 URL

// 封装 API 函数
export const api = {
    // 设备换绑接口
    async deviceBindings_rebind_api(oldMasterDeviceId, newMasterDeviceId, slaveDeviceId) {
      if (!oldMasterDeviceId || !newMasterDeviceId || !slaveDeviceId) {
        throw new Error('参数不完整');
      }ss
      
      try {
        const res = await new Promise((resolve, reject) => {
          uni.request({
            url: `${BASE_URL}/api/deviceBindings/rebind`,
            method: 'POST',
            header: {
              Authorization: token,  // 添加Authorization头部
            },
            data: { oldMasterDeviceId, newMasterDeviceId, slaveDeviceId },
            timeout: 10000,  // 设置超时时间 10秒
            success: resolve,
            fail: reject,
          });
        });
    
        if (res.statusCode === 200 && res.data.success) {
          return res.data;
        } else {
          throw new Error(res.data.message || '设备换绑失败');
        }
      } catch (err) {
        console.error('设备换绑失败:', err);
        throw err; // 抛出错误，供外部捕获
      }
    },
    
    // 设备注册接口
    async deviceBindings_register_api(masterDeviceId, slaveDeviceId) {
      if (!masterDeviceId || !slaveDeviceId) {
        throw new Error('参数不完整');
      }ss
      
      try {
        const res = await new Promise((resolve, reject) => {
          uni.request({
            url: `${BASE_URL}/api/deviceBindings/register`,
            method: 'POST',
            header: {
              Authorization: token,  // 添加Authorization头部
            },
            data: { masterDeviceId, slaveDeviceId },
            timeout: 10000,  // 设置超时时间 10秒
            success: resolve,
            fail: reject,
          });
        });
    
        if (res.statusCode === 200 && res.data.success) {
          return res.data;
        } else {
          throw new Error(res.data.message || '设备注册失败');
        }
      } catch (err) {
        console.error('设备注册失败:', err);
        throw err; // 抛出错误，供外部捕获
      }
    },
}